package com.sz.app.oss.api.logistics.base;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.logistics.base.service.AirlineService;
import com.sz.biz.logistics.base.dto.AirlineDto;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.utils.StringUtils;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Function: AirlineController <br>
 * Author: wentao.chang <br>
 * Date: 2017-08-04 15:19:00
 */
@RestController
@RequestMapping("${api}/biz/airlines")
@Api(description = " ", tags = "026-03、空运航线")
public class AirlineController extends BaseController {
    @Autowired
    private AirlineService airlineService;


    @ApiOperation(value = "添加空运航线", notes = "添加空运航线, <br>权限:" + Permissions.BIZ_MGT_BASE_LINE_AIR)
    @RequestMapping(value = "/", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_LINE_AIR)
    public ResultDto createAirlineModel(@RequestBody @ApiParam(value = "空运航线", required = true) AirlineDto model) {
        airlineService.save(model);
        return this.getSaveSuccessDto(MessagesCodes.MODULE_AIRLINE);
    }

    @ApiOperation(value = "修改空运航线", notes = "修改空运航线<br>权限：" + Permissions.BIZ_MGT_BASE_LINE_AIR)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "空运航线Id", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "model", value = "空运航线", required = true, dataType = "AirlineDto", paramType = "body")
    })
    @RequestMapping(value = "/{id}", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_LINE_AIR)
    public ResultDto updateAirlineModel(@PathVariable("id") int id, @RequestBody AirlineDto model) {
        airlineService.update(id, model);
        return this.getUpdateSuccessDto(MessagesCodes.MODULE_AIRLINE);
    }

    @ApiOperation(value = "批量删除空运航线", notes = "批量删除空运航线<br>权限：" + Permissions.BIZ_MGT_BASE_LINE_AIR)
    @RequestMapping(value = "/deletions", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_LINE_AIR)
    public ResultDto deleteAirlineModels(@RequestBody @ApiParam(value = "要删除的id集合", required = true) List<Integer> ids) {
        airlineService.deleteByIds(ids);
        return this.getDeleteSuccessDto(MessagesCodes.MODULE_AIRLINE);
    }

    @ApiOperation(value = "获取空运航线", notes = "获取空运航线<br>权限：")
    @ApiImplicitParam(name = "id", value = "空运航线Id", required = true, dataType = "int", paramType = "path")
    @RequestMapping(value = "/{id}", method = {RequestMethod.GET})
    public ResultDto getAirlineModel(@PathVariable("id") int id) {
        AirlineDto dto = airlineService.findById(id);
        return new ResultDto(dto);
    }

    @ApiOperation(value = "获取空运航线", notes = "获取空运航线<br>权限：")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "countryCodeFrom", value = "起运国家", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "triadCodeStart", value = "起运机场三字码", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "countryCodeTo", value = "目的国家", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "triadCodeEnd", value = "目的机场三字码", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "q", value = "查询关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "升序", dataType = "boolean", paramType = "query")
    })
    @RequestMapping(value = "/", method = {RequestMethod.GET})
    public ResultDto getAirlineModels(@RequestParam(required = false) String q,
                                      @RequestParam(required = false) String countryCodeFrom,
                                      @RequestParam(required = false) String triadCodeStart,
                                      @RequestParam(required = false) String countryCodeTo,
                                      @RequestParam(required = false) String triadCodeEnd,
                                      @RequestParam(defaultValue = "1") int pageIndex,
                                      @RequestParam(defaultValue = "20") int pageSize,
                                      @RequestParam(required = false ,defaultValue = "code") String sort,
                                      @RequestParam(defaultValue = "true") Boolean asc) {
        q = StringUtils.replaceSqlPattern(q);
        return airlineService.findAirlineListPage(new QueryParams(pageSize, pageIndex, q, sort, asc)
                .put("countryCodeFrom",countryCodeFrom)
                .put("triadCodeStart",triadCodeStart)
                .put("countryCodeTo",countryCodeTo)
                .put("triadCodeEnd",triadCodeEnd)
        );
    }

    @ApiOperation(value = "获取空运航线短接口", notes = "获取空运航线短接口<br>权限：")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "查询关键字", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "页码", dataType = "int", paramType = "query"),
    })
    @RequestMapping(value = "/short", method = {RequestMethod.GET})
    public ResultDto getAirlineModels(@RequestParam(required = false) String q,
                                      @RequestParam(defaultValue = "1") int pageIndex,
                                      @RequestParam(defaultValue = "10") int pageSize) {
        q = StringUtils.replaceSqlPattern(q);
        return airlineService.findAirlineShortListPage(new QueryParams(pageSize, pageIndex, q, null, true));
    }

    @ApiOperation(value = "检查空运航线名是否存在", notes = "检查空运航线名是否存在<br>权限：" + Permissions.BIZ_MGT_BASE_LINE_AIR)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "name", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "id", dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/checkname", method = {RequestMethod.GET})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_LINE_AIR)
    public ResultDto checkNameExist(@RequestParam(required = false) String name,
                                    @RequestParam(required = false) Integer id) {
        airlineService.checkName(name, id);
        return new ResultDto();
    }
    @ApiOperation(value = "检查空运航线编码是否存在", notes = "检查空运航线编码是否存在<br>权限：" + Permissions.BIZ_MGT_BASE_LINE_AIR)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "code", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "id", dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/checkcode", method = {RequestMethod.GET})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_LINE_AIR)
    public ResultDto checkCodeExist(@RequestParam(required = false) String code,
                                    @RequestParam(required = false) Integer id) {
        airlineService.checkCode(code, id);
        return new ResultDto();
    }
}
